package com.maduar.demo.lock;

import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.LockSupport;

/**
 * Description:
 *
 * @author danny.pang danny.pang@ebizprise.com
 * @date 2020-11-18 13:32
 */
public class FairLock2 {

    static Thread t1 = null;
    static Thread t2 = null;

    public static AtomicInteger sign = new AtomicInteger(0);

    public static void main(String[] args) {
        char[] num = "1234567".toCharArray();
        char[] str = "ABCDEFG".toCharArray();

        t1 = new Thread(() -> {
            for (char n : num) {
                while (sign.get() != 0) {
                }

                System.out.print(n);
                sign.set(1);
            }
        }, "t1");

        t2 = new Thread(() -> {
            for (char s : str) {
                while (sign.get() != 1) {
                }

                System.out.print(s);
                sign.set(0);
            }
        }, "t2");

        t1.start();
        t2.start();
    }
}
